<template>
  <div class="front shopHome">
    <div class="banner"
      :style="{backgroundImage:`url(${shopInfo.shopBackendImage?shopInfo.shopBackendImage:require('@/assets/front-img/shop/banner.png')})`}">
    </div>
    <div class="docWidth">
      <div class="category" v-if="!queryCategoryId">
        <span>分类</span>
        <ul>
          <li @click="changeCategory(0)" :class="categoryId === 0 ? 'active' : ''">全部</li>
          <li v-for="(item,index) in categoryList" :key="index" @click="changeCategory(item.id)"
            :class="categoryId === item.id ? 'active' : ''">{{item.name}}</li>
        </ul>
      </div>
      <CategoryProduct :categoryId="categoryId" />
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
import CategoryProduct from "../components/CategoryProduct";
import { pageVoByQo } from "@/api/front-home.js";
export default {
  name: 'FrontShopAllPro',
  components: { CategoryProduct },
  data() {
    return {
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        memberId: 0,
      },
      list: [],
      total: 0,
      shopId: '',
      categoryList: [],
      categoryId: 0,
      queryCategoryId: 0
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
    ...mapState({
      shopInfo: state => state.front.shopInfo,
    })
  },
  watch: {
    $route: {
      immediate: true,
      handler(val) {
        this.queryCategoryId = this.$route.query.categoryId && Number(this.$route.query.categoryId)
        this.categoryId = this.queryCategoryId || 0
        this.pageVoByQo()
      }
    },
  },
  created() {
  },
  methods: {
    pageVoByQo() {
      return new Promise((resolve, reject) => {
        pageVoByQo({ companyType: 1 })
          .then((res) => {
            this.categoryList = res;
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    changeCategory(id) {
      if (this.categoryId === id) {
        return
      }
      this.categoryId = id
    }
  },
};
</script>
<style lang="scss" scoped>
.shopHome {
  .banner {
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    height: 328px;
  }
  .category {
    margin-top: 20px;
    display: flex;
    line-height: 30px;
    padding: 10px 20px;
    font-size: 14px;
    color: #666;
    border: 1px solid rgb(246, 246, 246);
    span {
      font-size: 18px;
      color: #000;
    }
    ul {
      flex: 1;
      overflow: hidden;
      margin-bottom: -15px;
      li {
        float: left;
        cursor: pointer;
        margin-left: 55px;
        margin-bottom: 15px;
        &.active {
          color: #a60304;
        }
      }
    }
  }
}
</style>